package com.loong.news.controller.forehead;

import com.loong.common.mvc.BaseForeheadController;
import com.loong.common.param.BaseIdParam;
import com.loong.common.rest.Result;
import com.loong.common.validator.group.CommonInsert;
import com.loong.common.validator.group.CommonValidate;
import com.loong.news.common.forehead.param.DeleteNewsCommentParam;
import com.loong.news.common.forehead.param.NewsCommentParam;
import com.loong.news.handler.NewsCommentHandler;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Description: 处理前台的新闻评论的controller
 * @Author: 刘群
 * @Date: 2020/10/18 10:41
 */
@RestController
@RequestMapping("forehead/news/newsComment")
public class NewsCommentController extends BaseForeheadController {

    @Resource
    private NewsCommentHandler commentHandler;

    /**
     * 新增新闻评论
     * @param param 新增评论参数
     * @return 响应的结果
     */
    @PostMapping("save")
    public Result saveNewsComment(@RequestBody @Validated({CommonInsert.class}) NewsCommentParam param){
        param.setId(null);
        param.setUserId(super.getUserDTO().getId());
        return handlerExecutor.execute(() -> commentHandler.saveNewsComment(param));
    }

    /**
     * 删除评论
     * @param param 评论id和用户id封装的参数
     * @return 响应的结果
     */
    @PostMapping("delete")
    public Result deleteNewsComment(@RequestBody @Validated({CommonValidate.class})DeleteNewsCommentParam param){
        param.setUserId(super.getUserDTO().getId());
        return handlerExecutor.execute(() -> commentHandler.deleteNewsComment(param));
    }

    /**
     * 查看新闻的所有评论
     * @param param 新闻评论id封装的参数
     * @return 响应的结果
     */
    @PostMapping("select")
    public Result findNewsComment(@RequestBody @Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> commentHandler.findNewsComment(param.getId()));
    }

    /**
     * 点赞评论
     * @param param 新闻评论id封装的参数
     * @return 响应的结果
     */
    @PostMapping("like")
    public Result addLikeNum(@RequestBody @Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> commentHandler.addLikeNum(param.getId()));
    }

    /**
     * 举报评论
     * @param param param 新闻评论id封装的参数
     * @return 响应的结果
     */
    @PostMapping("unlike")
    public Result addUnLikeNum(@RequestBody @Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> commentHandler.addUnLikeNum(param.getId()));
    }

    /**
     * 更新评论的点赞举报数
     * @param param 新闻评论id封装的参数
     * @return 响应的结果
     */
    @PostMapping("updateLikeNumAndUnLikeNum")
    public Result updateLikeNumAndUnlikeNum(@RequestBody @Validated({CommonValidate.class})BaseIdParam param){
        return handlerExecutor.execute(() -> commentHandler.updateLikeNumAndUnlikeNum(param.getId()));
    }

}
